// componentlibrary.cpp
#include "componentlibrary.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTreeWidgetItem>
#include <QListWidgetItem>
#include <QListWidget>
#include <QIcon>
#include <QDebug>
#include <QMimeData>
#include <QDrag>
#include <QMouseEvent>

ComponentLibrary::ComponentLibrary(QWidget *parent) : QWidget(parent)
{
    qDebug() << "ComponentLibrary constructing";
    QVBoxLayout *layout = new QVBoxLayout(this);

    categoriesTree = new QTreeWidget(this);
    categoriesTree->setHeaderHidden(true);

    componentsList = new QListWidget(this);
    componentsList->setViewMode(QListWidget::IconMode);
    componentsList->setIconSize(QSize(64, 64));
    componentsList->setResizeMode(QListWidget::Adjust);
    componentsList->setMovement(QListWidget::Static);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->addWidget(categoriesTree, 1);
    hLayout->addWidget(componentsList, 3);

    layout->addLayout(hLayout);

    setupCategories();

    connect(categoriesTree, &QTreeWidget::currentItemChanged, [this](QTreeWidgetItem *current, QTreeWidgetItem *previous) {
        Q_UNUSED(previous)
        if (!current) return;

        componentsList->clear();
        QString category = current->text(0);

        for (Component *comp : componentMap[category]) {
            QListWidgetItem *item = new QListWidgetItem(comp->icon(), comp->name(), componentsList);
            item->setData(Qt::UserRole, QVariant::fromValue(comp));
        }
    });

    componentsList->setDragEnabled(true);
    componentsList->setDragDropMode(QAbstractItemView::DragOnly);
    qDebug() << "ComponentLibrary constructed with" << componentMap.size() << "categories";
}

void ComponentLibrary::setupCategories()
{
    // 导线与连接器
    QList<Component*> wires;
    wires << new Component("Red Wire", ":/components/wire_red.png", Component::Wire);
    wires << new Component("Blue Wire", ":/components/wire_blue.png", Component::Wire);
    wires << new Component("Black Wire", ":/components/wire_black.png", Component::Wire);
    // 添加更多连接器...
    componentMap["Wires & Connectors"] = wires;

    // 无源元件
    QList<Component*> passives;
    passives << new Component("Resistor", ":/components/resistor.png", Component::Passive);
    passives << new Component("Capacitor", ":/components/capacitor.png", Component::Passive);
    passives << new Component("Inductor", ":/components/inductor.png", Component::Passive);
    // 添加更多无源元件...
    componentMap["Passive Components"] = passives;

    // 半导体与开关
    QList<Component*> semiconductors;
    semiconductors << new Component("Diode", ":/components/diode.png", Component::Semiconductor);
    semiconductors << new Component("Transistor", ":/components/transistor.png", Component::Semiconductor);
    semiconductors << new Component("MOSFET", ":/components/mosfet.png", Component::Semiconductor);
    // 添加更多半导体...
    componentMap["Semiconductors & Switches"] = semiconductors;

    // 添加更多分类...

    // 创建树形分类
    categoriesTree->clear();
    for (const QString &category : componentMap.keys()) {
        QTreeWidgetItem *item = new QTreeWidgetItem(categoriesTree);
        item->setText(0, category);
    }

    categoriesTree->setCurrentItem(categoriesTree->topLevelItem(0));
}

void ComponentLibrary::addCategory(const QString &name, const QList<Component*> &components)
{
    componentMap[name] = components;
    QTreeWidgetItem *item = new QTreeWidgetItem(categoriesTree);
    item->setText(0, name);
}

void ComponentLibrary::clearSelection()
{
    componentsList->clearSelection();
    categoriesTree->clearSelection();
}

void ComponentLibrary::startDrag(Qt::DropActions supportedActions)
{
    QListWidgetItem *item = componentsList->currentItem();
    if (!item) return;

    QMimeData* mimeData = new QMimeData;
    mimeData->setText(item->text()); // 设置组件名称或唯一标识

    QDrag* drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(item->icon().pixmap(64, 64)); // 设置拖拽图标
    drag->setHotSpot(QPoint(32, 32)); // 设置图标中心点

    drag->exec(supportedActions);
}

Component* ComponentLibrary::getComponentByName(const QString &name)
{
    for (const auto &category : componentMap) {
        for (Component *comp : category) {
            if (comp->name() == name) {
                return comp;
            }
        }
    }
    return nullptr;
}

void ComponentLibrary::mouseDoubleClickEvent(QMouseEvent *event)
{
    // 判断双击事件是否发生在 componentsList 上
    if (componentsList->rect().contains(componentsList->mapFromParent(event->pos()))) {
        QListWidgetItem *item = componentsList->itemAt(componentsList->mapFromParent(event->pos()));
        if (item) {
            Component *comp = item->data(Qt::UserRole).value<Component*>();
            emit componentDoubleClicked(comp);
        }
    }

    // 调用父类的鼠标双击事件处理函数
    QWidget::mouseDoubleClickEvent(event);
}
